let arr = [1, 2, 3];
const arr1 = arr.slice(0);
// console.log(arr, arr1);

const arr2 = arr.concat();
// console.log(arr, arr2);

const arr3 = [...arr]; // es6新增  ...拓展运算符
// console.log(arr, arr3);

// 手动实现浅拷贝
function copy(obj) {
  if (obj instanceof Array) {
    let newArr = [];
    for (var i = 0; i < obj.length; i++) {
      newArr[i] = obj[i];
    }
    return newArr;
  } else if (obj instanceof Object) {
    let newObj = {};
    for (let key in obj) {
      newObj[key] = obj[key];
    }
    return newObj;
  }
}
// console.log(copy(["a", "b", "c"]));

// 手动实现深拷贝
function deepCopy(obj) {
  if (obj instanceof Array) {
    let newArr = [];
    for (var i = 0; i < obj.length; i++) {
      newArr[i] = deepCopy(obj[i]);
    }
    return newArr;
  } else if (obj instanceof Object) {
    let newObj = {};
    for (let key in obj) {
      newObj[key] = deepCopy(obj[key]);
    }
    return newObj;
  } else {
    return obj;
  }
}
console.log(
  deepCopy({ a: "a", b: "b", c: [1, 2, 3], d: { da: "da", db: "db" } })
);

// Object.assign({}, obj)  // 浅拷贝
// JSON.parse(JSON.stringify()); // 深拷贝  有坑  时间对象 正则表达式 函数 undefined NaN  等无法拷贝
// Lodash  有个方法  _cloneDeep可以实现深拷贝;
